class StudentController {
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    def allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        if(!params.max) params.max = 10
        [ studentList: Student.list( params ) ]
    }

    def show = {
        def student = Student.get( params.id )

        if(!student) {
            flash.message = "Student not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ student : student ] }
    }

    def delete = {
        def student = Student.get( params.id )
        if(student) {
            student.delete()
            flash.message = "Student ${params.id} deleted"
            redirect(action:list)
        }
        else {
            flash.message = "Student not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def student = Student.get( params.id )

        if(!student) {
            flash.message = "Student not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ student : student ]
        }
    }

    def update = {
        def student = Student.get( params.id )
        if(student) {
            student.properties = params
            if(!student.hasErrors() && student.save()) {
                flash.message = "Student ${params.id} updated"
                redirect(action:show,id:student.id)
            }
            else {
                render(view:'edit',model:[student:student])
            }
        }
        else {
            flash.message = "Student not found with id ${params.id}"
            redirect(action:edit,id:params.id)
        }
    }

    def create = {
        def student = new Student()
        student.properties = params
        return ['student':student]
    }

    def save = {
        def student = new Student(params)
        if(!student.hasErrors() && student.save()) {
            flash.message = "Student ${student.id} created"
            redirect(action:show,id:student.id)
        }
        else {
            render(view:'create',model:[student:student])
        }
    }
}
